package com.yxw.trade.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yxw.trade.dao.KlineMapper;
import com.yxw.trade.model.Kline;
import com.yxw.trade.model.KlineExample;
import com.yxw.trade.util.KlineFrame;

@Service
public class KlineService {
	@Autowired
	private KlineMapper klineMapper;
	
	public void listKline(){
		KlineExample ex = new KlineExample();
		ex.setOrderByClause("date asc");
		List<Kline> klines = klineMapper.selectByExample(ex);
		klines = mergeKline(klines);
		calcPen(klines);
		KlineFrame frame = new KlineFrame(klines);
		frame.setVisible(true);
	}
	
	/**
	 * 合并包含k线 计算分型
	 * @param klines
	 * @return
	 */
	public List<Kline> mergeKline(List<Kline> klines){
		if(klines == null || klines.size() == 0){
			return null;
		}
		List<Kline> ks = new ArrayList<Kline>();
		//趋势 true涨 down跌 默认涨
		boolean updown = true;
		//当前趋势 k 数量
		Kline prev = null;
		Kline current = null;
		for(int i = 1; i < klines.size(); i++){
			prev = klines.get(i-1);
			current = klines.get(i);
			if(updown){
				//需要合并包含的情况  当前的包含前一根k线
				if(prev.getLow() >= current.getLow() && prev.getHigh() <= current.getHigh()){
					current.setLow(prev.getLow());
					if(current.getOpen() > prev.getLow()){
						current.setOpen(prev.getLow());
					}
					if(current.getClose() > prev.getLow()){
						current.setClose(prev.getLow());
					}
					continue;
				}
				//需要合并被包含的情况  前一根包含当前k线
				if(prev.getLow() <= current.getLow() && prev.getHigh() >= current.getHigh()){
					current.setHigh(prev.getHigh());
					continue;
				}
				ks.add(prev);
				if(prev.getHigh() > current.getHigh()){
					prev.setType(1);
					updown = false;
				}
			}else{
				//需要合并包含的情况  当前的包含前一根k线
				if(prev.getLow() >= current.getLow() && prev.getHigh() <= current.getHigh()){
					current.setHigh(prev.getHigh());
					if(current.getOpen() > prev.getHigh()){
						current.setOpen(prev.getHigh());
					}
					if(current.getClose() > prev.getHigh()){
						current.setClose(prev.getHigh());
					}
					continue;
				}
				//需要合并被包含的情况  前一根包含当前k线
				if(prev.getLow() <= current.getLow() && prev.getHigh() >= current.getHigh()){
					current.setHigh(prev.getHigh());
					continue;
				}
				ks.add(prev);
				if(prev.getHigh() < current.getHigh()){
					
					prev.setType(2);
					updown = true;
				}
			}
		}
		return ks;
	}
	
	/**
	 * 计算笔
	 * @param klines
	 * @return
	 */
	public void calcPen(List<Kline> klines){
		if(klines == null || klines.size() == 0){
			return ;
		}
		//标记序号
		int tag = 1;
		//笔统计 计数
		int count = 0;
		//当前趋势 k 数量
		Kline current = null;
		for(int i = 1; i < klines.size(); i++){
			Kline k = klines.get(i);
			count++;
			if(current != null && count == 4){
				current.setTag(tag);
				current.setPen(1);
				tag++;
			}
			//是顶分型or底分型
			if(k.getType() > 0 ){
				if(count > 3){
					k.setTag(tag);
					k.setPen(1);
					tag++;
				}
				current = k;
				//重新计数
				count = 0;
			}
		}
	}
	
}
